<template>
  <el-card class="box-card" style="margin: 20px, 0px">
    <div slot="header" class="clearfix">
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visits"></el-tab-pane>
      </el-tabs>
      <div class="right">
        <span @click="setToDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
          value-format="yyyy-MM-dd"
          class="date"
        >
        </el-date-picker>
      </div>
    </div>

    <div>
      <el-row :gutter="10">
        <el-col :span="20">
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :span="4">
          <h3>门店{{ title }}排行</h3>
          <ul>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
            <li>
              <span class="index">1</span>
              <span>肯德基</span>
              <span class="value">2343</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import moment from "moment";
import dayjs from "dayjs";
export default {
  name: "",
  data() {
    return {
      activeName: "sale",
      myCharts: null,
      date: [],
    };
  },

  methods: {
    setToDay() {
      let day = moment().format().substring(0, 10);
      this.date = [day, day];
    },

    setWeek() {
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      console.log(start, end);
      this.date = [start, end];
    },

    setMonth() {
      let start = dayjs().startOf("month").format("YYYY-MM-DD");
      let end = dayjs().endOf("month").format("YYYY-MM-DD");
      console.log(start, end);
      this.date = [start, end];
    },
    setYear() {
      let start = dayjs().startOf("year").format("YYYY-MM-DD");
      let end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },

  computed: {
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
  },

  watch: {
    title() {
      this.myCharts.setOption({
        title: {
          text: this.title,
        },
      });
    },
  },

  mounted() {
    this.myCharts = echarts.init(this.$refs.charts);
    this.myCharts.setOption({
      title: {
        text: "销售额趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",

        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          color: "pink",
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [32, 52, 200, 334, 390, 330, 220, 324, 234, 134, 314, 122],
        },
      ],
    });
  },
};
</script>

<style  lang="less" scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.tab {
  width: 100%;
}

.right {
  position: absolute;
  right: 0px;
  span {
    margin: 0px 10px;
  }
}
.date {
  margin: 0px 20px;
  width: 300px;
}

.charts {
  margin-top: 10px;
  width: 100%;
  height: 300px;
}

ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
  li {
    height: 8%;
    width: 100%;
    margin-top: 10px;
  }
}

.index {
  display: inline-block;
  margin-right: 20px;
  text-align: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  line-height: 20px;
  background: black;
  color: #eee;
}

.value {
  float: right;
}
</style>